<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mobile_Line_module extends CI_Module
{
    public $data;
    public $sid ;
    public function __construct(){
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data =$GLOBALS['config'];
        $this->load->model('member.User_model');
       
       
      
        
        
        
        $this->load->model('admin.Adminuser_model');
       
        
       
        $this->load->model('scenic.scenic_model');
        $this->load->model('admin.area_model');
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordertourist_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Ordergodateprice_model');
        $this->load->model('order.Ordergodate_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Orderticket_model');
        
        $this->load->model('expert.Expert_model');
        $this->load->model('line.Line_fx_go_price_model');
       
      
        
        
        
        $this->load->model('admin.Adminuser_model');
       
        
       
        $this->load->model('line.Line_scenic_model');
        $this->load->model('scenic.scenic_model');
        $this->load->model('admin.area_model');
        
        
        $this->load->model('expert.Expert_model');
        $this->load->model('line.Goods_model');
        $this->load->model('line.Goods_cat_model');
        $this->load->model('line.Line_view_model');
        $this->load->model('comment.comment_model');
        
        $this->load->model('weixin.Worder_tel_model');
        $this->load->model('weixin.weixin_model');


        $user_id = $this->session->userdata('user_id');
        if(!$user_id) redirect('mobile/member/oauth');
    }

    public function index()
    {
        $keyword = $where = '';
        $cat_id = '0';
        $pagesize = 8;
        $city = CURRENT_CITY_NAME ;
        $multicity = FALSE;
        $ar  = array_filter(explode(',', trim($GLOBALS['config']['depart_citys'], ',')));
        foreach ($ar as $key => $item) {
            if($item==-1){
                unset($ar[$key]);
            }
        }
        if(count($ar)>=2){
            $multicity = TRUE;
        }else{
            $multicity = FALSE;
        }
        if(!CURRENT_CITY_ID){
            $multicity = false;
        }
        if($_GET['touchtpl']!='weixin'){
            $multicity = false;
        }
        $page = !empty($_GET['page']) && $_GET['page']>0 ? intval($_GET['page']) : 1;

        $price_array = array(
            '0-0' => array(
                'name' => '不限',
                'link_url' => '0-0',
            ),
            '0-200' => array(
                'name' => '￥0~￥200',
                'link_url' => '0-200',
            ),
            '200-500'=> array(
                'name' => '￥200~￥500',
                'link_url' => '200-500',
            ),
            '500-1000' => array(
                'name' => '￥500~￥1000',
                'link_url' => '500-1000',
            ),
            '1000-2000' => array(
                'name' => '￥1000~￥2000',
                'link_url' => '1000-2000',
            ),
            '2000-9999999' => array(
                'name' => '￥2000及以上',
                'link_url' => '2000-9999999',
            ),
        );
        list($minprice, $maxprice) = explode('-', trim($_GET['price']));
        $minprice = intval($minprice);
        $maxprice = intval($maxprice);
        $condition = array(
                           'page'=>$page ,
                           'page_size'=>10,
                           'cat_id'=>intval($_GET['cat_id']),
                           'expand_1'=>0,
                           'd_city'=>intval($_GET['dcity']),
                           'city'=>intval($_GET['city']),
                           'product_sort'=>0,
                           'keywords'=>'',
                           'mark'=>0,
                           'sid'=>$this->sid,
                           'travel_days' => intval($_GET['day']),
                            'minprice' => $minprice,
                            'maxprice' => $maxprice,
                       );

        if(!empty($_GET['keyword'])){
            $keyword = addslashes($_GET['keyword']);
            $condition['keywords'] = $keyword ;
        }

        if($_GET['cat_id']!=='0'){
            $cat_id = addslashes($_GET['cat_id']);
            $condition['cat_id'] = intval($cat_id) ;
        }

        if($_GET['cat_id']=='1')
        {
            $cat_name = '国内游';
            $link_url = 'guoneiyou';
            $head = '国内线路' . ($keyword ? '-'.$keyword : $keyword);
            $title = $city.'国内旅游_国内线路_'.$GLOBALS['config']['sitename'];
            $keywords = $city.'国内旅游,'.$city.'国内线路,'.$city.'国内旅游好去处';
            $description = $city.'国内旅游，'.$city.'旅游网为您提供优质的一站式国内旅游预订服务。'.$GLOBALS['config']['sitename'];
        }

        if($_GET['cat_id']=='2')
        {
            $cat_name = '周边游';
            $link_url = 'zhoubianyou';
            $head = '周边线路' . ($keyword ? '-'.$keyword : $keyword);
            $title = $city.'周边旅游_周边线路_'.$GLOBALS['config']['sitename'];
            $keywords = $city.'周边旅游,'.$city.'周边线路,'.$city.'周边旅游好去处';
            $description = $city.'周边旅游，'.$city.'旅游网为您提供优质的一站式周边旅游预订服务。'.$GLOBALS['config']['sitename'];

        }

        if($_GET['cat_id']=='3')
        {
            $cat_name = '出境游';
            $link_url = 'chujingyou';
            $head = '出境线路' . ($keyword ? '-'.$keyword : $keyword);
            $title = $city.'出境旅游_出境线路_'.$GLOBALS['config']['sitename'];
            $keywords = $city.'出境旅游,'.$city.'出境线路,'.$city.'出境旅游好去处';
            $description = $city.'出境旅游，'.$city.'旅游网为您提供优质的一站式出境旅游预订服务。'.$GLOBALS['config']['sitename'];
        }

        $row = $this->Recommendlinecat_model->get_row(array('link_url'=>$link_url,'sid'=>$this->sid));
        $select_categorys = $this->Recommendlinecat_model->get_tree_array($row['cat_id']);
        $select_categorys = array_merge(array(

            0=> array(
                'name' => '价格范围',
                'link_url' => 'fanwei',
                'child' => $price_array
            )
        ),

            $select_categorys);
        $this->data['select_categorys'] = $select_categorys;

        /*筛选*/

        if($_GET['cat'] && $catinfo = $this->Recommendlinecat_model->get_row(array('link_url'=>$_GET['cat']),'cat_id,cat_name,parent_id,line_sort_id,line_cat_id,cat_type,cat_value')) {
            $head = $city . '到'.$catinfo['cat_name'] .'旅游';
            $where = array(
                'cat_id' => $catinfo['cat_id'],
                'line_sort_id' => $catinfo['line_sort_id'],
                'line_cat_id' => $catinfo['line_cat_id'],
                'cat_type' => $catinfo['cat_type'],
                'cat_value' => $catinfo['cat_value'],
                'days' => intval($_GET['day']),
//                'mark' => $mark,
//                'start_time' => $start_time,
//                'end_time' => $end_time,
                'page' => $page,
                'minprice' => $minprice,
                'maxprice' => $maxprice,
//                'keywords' =>$keyword,
//                'url' => $url . '/list-' . $days . '-' . $start_time . '-' . $end_time . '-' . $city . '-' . $mark.'-'.$keyword
            );
            if ($_GET['city']) {
                $where['d_city'] = intval($_GET['dcity']);
            }
            if($multicity){
                $where['city'] = CURRENT_CITY_ID;
            }
            $line = $this->Recommendlinecat_model->get_where_list($where);
            $res = array('error_code' => '0', 'error_msg' => '', 'total' => $line['page_config']['total'], 'data' => $line['line_list']);
            if (empty($list)) {
                $res['error_code'] = '1';
                $res['error_msg'] = '暂无数据';
            }
            $result = $res;
            $this->data['fcat_name'] = $catinfo['cat_name'];
        }else if($mark = trim($_GET['mark'])){
            $where['mark'] = "'".addslashes($mark)."'";

            if($multicity){
                $where['city'] = CURRENT_CITY_ID;
            }
            $where['page'] = $page;
            $line = $this->Recommendlinecat_model->get_where_list($where);
            $res = array('error_code' => '0', 'error_msg' => '', 'total' => $line['page_config']['total'], 'data' => $line['line_list']);

            if (empty($list)) {
                $res['error_code'] = '1';
                $res['error_msg'] = '暂无数据';
            }
            $result = $res;

            $head = $city.'旅行社'.(is_numeric($mark) ? '' : $mark);
            $title = $city.'旅行社_附近景点_'.$GLOBALS['config']['sitename'];
            $keywords = $city.'旅游,'.$city.'附近景点,'.$city.'旅游好去处';
            $description = $city.'旅游，'.$city.'旅游网为您提供优质的一站式旅游预订服务。'.$GLOBALS['config']['sitename'];


        }else{
            $append = '';
            if($multicity){
                $append = ' a.city='.CURRENT_CITY_ID;
            }
            $result = $this->Line_model->get_list_m($condition, $append);
        }
        $this->data['cat']  = $_GET['cat'];
        $this->data['datalist']  = $result['data'];
        $this->data['keyword'] = $keyword;
        $this->data['cat_id'] = $cat_id;
        $this->data['cat_name'] = $cat_name;
        $this->data['page'] = $page;
        $this->data['pricedis'] = $price_array[$_GET['price']];
        foreach ($this->data['datalist'] as $k => $v)
        {
            $this->data['datalist'][$k]['file_url']  = filter_img($v['line_thumb']);
            $this->data['datalist'][$k]['link']      = site_url('mobile/line/profile/?id=' . $v['line_id'].'&eid='.$_COOKIE['from_card']);
            $this->data['datalist'][$k]['low_price'] = $v['low_price'] == '￥0' || empty($v['low_price']) ? '电询' : '￥'.$v['low_price'].'<span>起</span>';
            $this->data['datalist'][$k]['sold'] = intval($v['order_total']);
            $this->data['datalist'][$k]['error_image'] =  URLPRE.'static/public/images/nophoto.jpg';
            $this->data['datalist'][$k]['original'] =  ($v['low_price'] == '￥0' || empty($v['low_price']))?'电询':'￥'.intval($v['low_price']+$v['youhui']);

            $ps = get_product_sort_name($v['product_sort']);
            $this->data['datalist'][$k]['sort_name'] = $ps ? $ps : '';
        }
        $this->data['show_more'] = '';
        $this->data['common'] = array(
            array(
                'head'   => $head ? $head : (isset($_GET['keyword']) ? '搜索结果-'.$keyword : '线路列表') ,
                'title'   => $title,
                'keywords'   => $keywords,
                'description'   => $description
            )
        );
        if(!empty($_GET['isajax']))
        {
            header('Content-type:application/json;charset=utf-8');
            echo json_encode($this->data['datalist']);
            exit;
        }
        else
        {
            $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'line', $this->data);
        }
    }

    public function profile()
    {
        $id = intval($_GET['id']);
        //访问量
        $this->Line_model->update_visits($id);
        $this->Line_view_model->update_data('views');

        if($_GET['eid']){
            $user_id = intval($_GET['eid']);
            $this->data['info'] = $this->Expert_model->_expert_info($user_id);

            $mobile = $this->data['info']['mobile'] ? $this->data['info']['mobile'] : $this->data['site_service_phone'];
            $expert_company = $this->data['info']['expert_company'] ? $this->data['info']['expert_company'] : $this->data['company'];

            set_cookie('from_card', $user_id, 315360000);
            set_cookie('from_tel', $mobile, 315360000);
            set_cookie('card_info', serialize($this->data['info']), 315360000);
            set_cookie('expert_company', $expert_company, 315360000);

            $_COOKIE['from_card'] = $user_id;
            $_COOKIE['from_tel'] = $mobile;
            $_COOKIE['expert_company'] = $expert_company;
            $_COOKIE['card_info'] = serialize($this->data['info']);
        }

        $this->data['row']  = $row = $this->Line_model->get_row(array('line_id'=>$id));
        $this->data['row']['low_price'] =  $this->Line_go_price_model->get_min_price($id);
        $this->data['row']['dist']  = $this->Line_model->get_last_area($row['d_continent'],$row['d_country'],$row['d_province'],$row['d_city']);
        $this->data['row']['product_sort_name']  = $this->Line_model->get_product_sort_name($row['product_sort']);

        $this->data['info']       = $this->Line_info_model->get_row(array('line_id'=>$id));
        $this->data['venue']      = $this->Line_venue_model->get_list('line_id='.$id);
        $this->data['days']       = $this->Line_day_model->get_list('line_id='.$id);
        $ahead = $this->data['row']['ahead_enroll'];
        $this->data['go']         = $this->Line_go_model->get_list("go_date>'".$date."' and line_id=".$id,'go_id,line_id,go_date,seat_num,group_num');
        foreach ($this->data['go'] as $k => $go) {
            $this->data['go'][$k]['price_list'] = $price_list = $this->Line_go_price_model->get_list(array('go_id'=>$go['go_id']));

            foreach ($price_list as $v) {
                if($v['fee']> 0){
                    $this->data['fee'][] = $v['fee'];
                }
            }

        }
        if($this->session->userdata('admin_user_id')){
            if(empty($this->data['fee'])){
                $fee = 0;
            }else{
                $fee = intval(max($this->data['fee']));
            }
            $this->data['fee'] = $fee;
            $this->data['fee_string'] = '<span class="sale">佣金:' .$fee.'</span>';

            $expert_info = $this->Expert_model->_expert_info(ADMINID);
        }

        /*是否显示预定按钮*/
        $display = $GLOBALS['config']['mobileorderdisable'];  //游客可以预定
        if(ADMINID && $expert_info['pid'] ==0){  //管理员不可预定
            $display = 0;
        }elseif (ADMINID && $expert_info['pid']!=0){  //二级微名片可以预定
            $display = 1;
        }
        $this->data['display'] = $display;

        $current            = ($this->data['row']['d_city']==1614)?'line':'chuyou';



        $conn = $this->Line_model->get_line_row(array('line_id'=>$id),'line_id,conn_line_id,is_del,sid');
        $fx = $this->Line_fx_go_price_model->get_row(array('line_id'=>$id,'sid'=>$GLOBALS['sid']));
        if($conn['is_del']==1){
            header("location:/");
            exit;
        }
        if($conn['sid']!=$GLOBALS['sid']&&empty($fx)){
            header("location:/");
            exit;
        }
        $conn_line_id = empty($conn['conn_line_id'])?$id:$conn['conn_line_id'];
        $conn_line_id = intval($conn_line_id);
        $data['row']        = $this->Line_model->get_row(array('line_id'=>$conn_line_id));
        $data['row']['line_id'] = $conn['line_id'];
        $data['row']['sid']      = $conn['sid'];
        $ahead = $data['row']['ahead_enroll'];
        $info       = $this->Line_info_model->get_row(array('line_id'=>$conn_line_id));
        $info['line_trait'] = nl2br($info['line_trait']);
        $info['costin'] = nl2br($info['costin']);
        $info['costout'] = nl2br($info['costout']);
        $info['enroll_know'] = nl2br($info['enroll_know']);
        $info['remind'] = nl2br($info['remind']);
        $info['ddzx'] = nl2br($info['ddzx']);
        $info['aqwt'] = nl2br($info['aqwt']);
        $info['cydh'] = nl2br($info['cydh']);
        $info['xqzb'] = nl2br($info['xqzb']);
        $info['zftj'] = nl2br($info['zftj']);
        $data['info'] = $info;
        $data['days']       = $this->Line_day_model->get_list('line_id='.$conn_line_id);
        foreach($data['days'] as $key=>$val){
            $data['days'][$key]['info'] = nl2br($val['info']);
        }
        $date = date('Y-m-d',strtotime("+$ahead day"));//提前多少天可预定

        //分销线路价格团期
        if(!empty($fx)){
            $data['row']['low_price'] = $this->Line_fx_go_price_model->get_min_price($conn_line_id,$ahead);
            $data['go'] = $this->Line_go_model->fx_go_list($conn_line_id,$ahead);

        }else{
            $data['row']['low_price'] =  $this->Line_go_price_model->get_min_price($conn_line_id,$ahead);
            $data['venue']      = $this->Line_venue_model->get_list('line_id='.$conn_line_id);

            $data['go']         = $this->Line_go_model->get_list("go_date>='".$date."' and line_id=".$conn_line_id,'go_id,line_id,go_date,seat_num,group_num');
            foreach($data['go'] as $key=>$val){
                $price = $this->Line_go_price_model->get_list(array('go_id'=>$val['go_id'],'sid'=>$info['sid']));
                $go_price = 0;
                $str = '';
                foreach($price as $v){
                    $str.=$v['price_name'].'￥'.$v['ws_price'].'起&nbsp;';
                    if($v['cate_attr']==1&&($v['ws_price']>$go_price)){
                        $go_price = $v['ws_price'];
                    }
                }
                $data['go'][$key]['price'] = $str;
                $data['go'][$key]['go_price'] = $go_price;
            }
        }

        $this->data = array_merge($this->data, $data);
        $this->data['conn_lines'] = $this->data['row']['conn_lines'];
        $city = SITECITYNAME ;
        $d_city = $this->area_model->get_area_name(array($this->data['row']['d_city']));
        $line_name = $this->data['row']['line_name'];
        $title = $line_name;
        if($this->sid!=33664){$title.='_'.$GLOBALS['config']['sitename'];}
        $this->data['common'] = array(
            array(
                'head'   => $line_name,
                'title'   => $title,
                'keywords'   => $city.'到'.$d_city.'旅游,'.$city.'到'.$d_city.'旅游报价,'.$city.'出发到'.$d_city.'旅游',
                'description'   => $GLOBALS['config']['sitename'].'提供最及时全面的'.SITECITYNAME.'到'.$line_name.'旅游线路及报价,优惠的价格, 优质的服务，免费咨询。'.$GLOBALS['config']['sitename']
            )
        );

        $incrurl = site_url('mobile/member/card/?eid='.$user_id.'&incr=1');
        if(DEBUG){
            $debug = 'true';
        }else{
            $debug = 'false';
        }
        $description   =   $this->data['row']['low_price'] ? '￥' .  $this->data['row']['low_price'] . '起' : '电询';
        foreach ($this->data['go'] as $k => $v) {
            $description_ary[] = $v['go_date'] . $v['week'];
        }
        if($description_ary){
            $description .= '，'. implode('、', $description_ary);
        }

        $this->setting = $this->weixin_model->db_get_setting();
        $appid         = $this->setting['appid'];
        $appsecret     = $this->setting['appsecret'];
        $geturl        = get_url();
        $noncestr      = uniqid('wx');
        $timestamp     = time();
        $signature     = __get_signature($appid, $appsecret, $noncestr, $geturl, $timestamp);
        $imgurl        = filter_img($row['gallery'][0]['url']);

        $linesharehtml = <<<HTML
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
var title='$line_name', desc='$description', link='$geturl',imgUrl='$imgurl';
wx.config({
    debug:$debug,
    appId: '$appid',
    timestamp: $timestamp,
    nonceStr: '$noncestr',
    signature: '$signature',
    jsApiList: [
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo'
    ]
});
function todoaftershare() {
  $.get('$incrurl', function(){
  });
}
wx.ready(function () {
    wx.onMenuShareAppMessage({title: title,desc: desc,link: link,imgUrl: imgUrl,success:function(){    todoaftershare();}});
    wx.onMenuShareTimeline({title: title,link: link,imgUrl: imgUrl,success:function(){    todoaftershare();}  });
    wx.onMenuShareQQ({title: title,desc: desc,link: link,imgUrl: imgUrl,success:function(){    todoaftershare();}   });
    wx.onMenuShareWeibo({title: title,desc: desc,link: link,imgUrl: imgUrl,success:function(){    todoaftershare();}   });
});
</script>
HTML;
        $this->data['linesharehtml'] = $linesharehtml;
        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'line_profile', $this->data);
    }

    public function go_date()
    {
        $go_id = intval($this->input->get_post('goid'));
        if(!$go_id){
            return '[]';
        }
        $price_list = $this->Line_go_price_model->get_list(array('go_id'=>$go_id));
        $price_list = array_values($price_list);
        if($price_list) return json_encode($price_list);
    }

    public  function order_tel(){
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $param['line_id'] = $this->input->get_post('line_id');
            $param['line_name'] = $this->input->get_post('line_name');
            $param['tel'] = $this->input->get_post('phone');
            $param['sid'] = $this->sid;
            $param['add_time'] = date('Y-m-d H:i:s');

            $res = $this->Worder_tel_model->add_row($param);
            if($res){
                return json_encode(array('status'=>'1','info' => '留言成功'));
            }else{
                return json_encode(array('status'=>'0','info' => '留言失败'));
            }
        }
        $line_id = $_GET['id'];
        $this->data['row'] = $this->Line_model->get_line_row(array('line_id'=>$line_id),'line_name,line_id');
        $this->data['form_url'] = site_url('mobile/line/order_tel');
        $this->data['common'] = array(
            array(
                'noback' => false,
                'head'   => '咨询',
                'title'   => '咨询',
            )
        );

        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'line_order_tel', $this->data);

    }
    public function order()
    {
        $check_login = FALSE;
        @$user_id = $this->session->userdata('user_id');
        @$admin_user_id = ADMINID;//intval( get_cookie('from_card'));

        if($user_id || $admin_user_id){
            $check_login = TRUE;
        }
        if(!$check_login){
            redirect(site_url('mobile/member/login?referer='.$_GET['referer']));
        }

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
//            if($admin_user_id)  return json_encode(array('status'=>'0','info' => '达人身份不可以购买,请使用普通账户登陆'));
            $user_id = @intval($this->session->userdata('user_id'));
            $param['line_id'] = $this->input->get_post('line_id');
            $param['go_id'] = $this->input->get_post('go_id');
            $param['contact_phone_num'] = $this->input->get_post('phone');
            $param['contact_name'] = $this->input->get_post('name');
            $param['contact_email'] = $this->input->get_post('email');
            $param['order_note'] = '';
            $go_num = $this->input->get_post('go_num');
            $price_id = $this->input->get_post('price_id');
            if(!empty($go_num)&&!empty($price_id))
            {
                $go_num = explode(',',$go_num);
                $price_id = explode(',',$price_id);
                foreach($go_num as $key=>$val)
                {
                    $param['price_list'][$key] = array(
                        'price_id'=> $price_id[$key],
                        'num' =>$val,
                    );
                }
            }

            $order = $this->get_order_info($param);

            //订单信息
            $order_data = array(
                'sid'=>$this->sid ,
                'order_sn' => $this->Orderinfo_model->set_order_sn(),
                'order_type' => 1,//线路订单
                'cat_type' => ($admin_user_id && !$user_id) ? 6 : 6,//1门市订单，6手机订单 都为手机订单算了
                'order_status' => 1,//未审核
                'pay_status' => 1,//未支付
                'add_time' => date('Y-m-d H:i:s',now()),
                'paid_account' => '0.00',//已支付金额
                'order_account' => $order['total_price'],//订单总金额
                'back_account'=>$order['fee_account'],//佣金
                'order_note' => $order['contact_info']['order_note'],//订单备注
                'user_id' => $user_id,//用户ID
                'admin_user_id' => $admin_user_id,//管理员ID
            );
            if($GLOBALS['config']['order_tel_check']==0){
                $order_data['order_status'] = 2;
            }
            $order_id = $order_return_status = $this->Orderinfo_model->add_row($order_data);

            if($order_id){
                //联系人信息保存
                $contact_data = array(
                    'order_id' => $order_id,
                    'name' => $order['contact_info']['name'],
                    'email' =>$order['contact_info']['email'],
                    'phone_num' =>$order['contact_info']['phone_num'],
                );
                $contact_return_status = $this->Ordercontacts_model->add_row($contact_data);

                //订单团期表保存
                $go_date = array(
                    'order_id' => $order_id,
                    'sid'=>$this->sid ,
                    'go_id' => $order['go_info']['go_id'],//团期ID
                    'go_date' => $order['go_info']['go_date'],//发团日期
                    'line_id' => $order['line_info']['line_id'],//线路ID
                    'line_name' => $order['line_info']['line_name'],//线路名称
                    'line_number' => $order['line_info']['line_number'],//线路编号
                    'departure_city' => $order['line_info']['departure_city'],//出发城市
                    'total_num' => $order['total_num'],//总人数
                    'single_room_type' => $order['single_room_type'],//选择类型
                    'dif_price' => $order['go_info']['dif_price'],//单房差价格
                    'collection_site' => '',//集合地点
                );
                $go_date_return_status = $this->Ordergodate_model->add_row($go_date);

                //订单团期价格
                $price_data = array();
                if(isset($order['price_list']))
                {
                    foreach($order['price_list'] as $val)
                    {
                        $row = $val;
                        $row['order_id'] = $order_id;
                        $price_data[] = $row;
                    }
                }
                $price_data_return_status = $this->Ordergodateprice_model->add_batch($price_data);

                if($contact_return_status&&$go_date_return_status&&$price_data_return_status)
                {
                    //删除线路订单
                    $this->session->unset_userdata('line_cart');

                    //发送短信
                    $site = $GLOBALS['config'];
                    $line_order_template = $site['line_order_template_submit'];

                    //短信内容
                    $content = str_replace(array('{$order_sn}','{$line_name}','{$go_date}','{$contact_name}','{$contact_phone}','{$order_account}'),array($order_data['order_sn'],
                        $go_date['line_name'],$go_date['go_date'],$contact_data['name'],$contact_data['phone_num'],$order['total_price']),$line_order_template);
                    $this->User_model->_send_sms($contact_data['phone_num'],$content);
                    $admin_id = intval(get_cookie('from_card')) ? intval(get_cookie('from_card')) : intval($_COOKIE['from_card']) ;
                    $admin_info = $this->Adminuser_model->get_row(array('id'=>$admin_id));


                    $template = $this->data['sms_template_order'];
                    $admin_content = str_replace(array('{$order_sn}','{$line_name}','{$go_date}','{$contact_name}','{$contact_phone}','{$order_account}'),array($order_data['order_sn'], $go_date['line_name'],$go_date['go_date'],$contact_data['name'],$contact_data['phone_num'],$order['total_price']),$template);

                    $admin_tel = ($admin_info['mobile'] ? $admin_info['mobile'] : $_COOKIE['from_tel']);
                    if($admin_tel != $GLOBALS['site']['site_service_phone']){
                        $this->User_model->_send_sms($admin_tel,$admin_content);
                    }else{
                        $this->User_model->_send_more_sms($GLOBALS['config']['notify_num'],$admin_content);
                    }
                    return json_encode(array('status'=>'1','info' => '订单提交成功','order_id'=>$order_id));
                }
                else
                {

                    return json_encode(array('status'=>'0','info' => '订单提交失败'));
                }
            }

        }


        $id = intval($_GET['id']);
        $this->data['row']       = $row = $this->Line_model->get_row(array('line_id'=>$id));

        $ahead = $this->data['row']['ahead_enroll'];
        $date = date('Y-m-d',strtotime("+$ahead day"));//提前多少天可预定

        $this->data['row']['low_price'] =  $this->Line_go_price_model->get_min_price($id);
        $chufa = explode('][',$this->data['row']['chufa']);
        $this->data['row']['chufa_city'] = strtr($chufa[1],array(']'=>''));

        $this->data['info']       = $this->Line_info_model->get_row(array('line_id'=>$id));
        $this->data['venue']      = $this->Line_venue_model->get_list('line_id='.$id);
        $this->data['go']         = $this->Line_go_model->get_list("go_date>'".$date."' and line_id=".$id,'go_id,line_id,go_date,seat_num,group_num');

        if($row['conn_line_id']){
            $this->data['days']       = $this->Line_day_model->get_list('line_id='.$row['conn_line_id']);
            $this->data['go']         = $this->Line_go_model->get_list("go_date>'".$date."' and line_id=".$row['conn_line_id'],'go_id,line_id,go_date,seat_num,group_num');
        }


        $this->data['form_url'] = site_url('mobile/line/order');
        $this->data['go_url'] = site_url('mobile/line/go_date');
        $this->data['common'] = array(
            array(
                'noback' => false,
                'head'   => '填写线路订单',
                'title'   => '填写线路订单',
            )
        );

        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'line_order', $this->data);
    }

    public function filter()
    {
        $cat_id = $_GET['cat_id'];
        $where = array(
            'cat_id' => $cat_id,
//            'line_sort_id' => 0,
//            'cat_type'=>$row['cat_type'],
//            'cat_value'=>$row['cat_value'],
//            'days' => $days,
//            'city' => $city,
//            'mark' => $mark,
//            'start_time' => $start_time,
//            'end_time' => $end_time,
//            'page' => $page,
//            'url' => $url . '/list-' . $days . '-' . $start_time . '-' . $end_time . '-' . $city . '-' . $mark
        );
//        $line = $this->Recommendlinecat_model->get_where_list($where);
//        $this->data = array_merge($line, $this->data);
        $res = $this->Line_model->ZG_load_departure_dist($cat_id);
        $filter = $res[$cat_id];
        $departure_city_id = $filter['city'];
        $dist_city_id      = $filter['d_city'];
        $travel_days       = $filter['travel_days'];
        sort($travel_days);
        $departure_city = $this->area_model->get_area_name($departure_city_id, FALSE);
        $dist_city      = $this->area_model->get_area_name($dist_city_id, FALSE);
        $dist_city = $dist_city + $this->area_model->get_area_name($filter['d_province'], FALSE);

        $this->data['departure_city'] = $departure_city;  //出发城市
        $this->data['dist_city']      = $dist_city;      //目的地城市/省份
        $this->data['travel_days']    = $travel_days;      //形成天数
        $this->load->view(MOBILE_TEMPLATE_NAME.'/filter', $this->data);
    }


    /**
     * 根据传值获取订单基本信息
     * @param $arg_post
     */
    public function get_order_info($arg_post){


        //线路信息
        $line = $this->Line_model->get_row(array('line_id'=>$arg_post['line_id']));

        //出发省份城市
        $departure_city = trim($this->area_model->get_area_name(array($line['province'],$line['city'])), '');
        $departure_city = str_replace(array('[',']'),'',$departure_city);
        $line['departure_city'] = $departure_city;

        //团期信息
        $go_info = $this->Line_go_model->get_row(array('go_id'=>$arg_post['go_id']),'go_id,go_date,dif_price');

        //价格类型
        $price_list = array();
        $total_price=0;
        $total_num = 0;
        $fee_account = 0;
        if(!empty($arg_post['price_list'])){
            foreach($arg_post['price_list'] as $key=>$val){
                $price_info = $this->Line_go_price_model->get_row(array('price_id'=>$val['price_id']));
                $val['price_name'] = $price_info['price_name'];
                $val['price'] = $price_info['ws_price'];
                $price_list[] = $val;
                $total_num   += $val['num'];
                $total_price += $val['price']*$val['num'];
                $fee_account += $price_info['fee'] * $val['num'];
            }
        }

        //联系人信息
        $contact_data = array();
        if(isset($arg_post['contact_name'])){
            $contact_data = array(
                'name' => $arg_post['contact_name'],
                'email' => $arg_post['contact_email'],
                'phone_num' => $arg_post['contact_phone_num'],
                'order_note' =>$arg_post['order_note'],
            );
        }

        //单房差信息
        $single_room_type = '1';
        if(isset($arg_post['single_room_type'])){
            $single_room_type = $arg_post['single_room_type'];
        }

        //游客信息
        $tourist_data = array();
        if(isset($arg_post['name'])){
            foreach($arg_post['name'] as $key=>$val){
                $row = array(
                    'name' => $val,
                    'phone_num' => $arg_post['phone_num'][$key],
                    'id_card_type' => $arg_post['id_card_type'][$key],
                    'id_number' => $arg_post['id_number'][$key],
                );
                $tourist_data[] = $row;
            }
        }

        $data = array(
            'line_info' => $line,
            'go_info' => $go_info,
            'price_list' => $price_list,
            'contact_info' =>$contact_data,
            'single_room_type' => $single_room_type,
            'tourist_data' => $tourist_data,
            'total_price' =>$total_price,
            'total_num' => $total_num,
            'fee_account'=>$fee_account,
        );
        return $data;
    }

    public function line_city(){
        $this->data['common'] = array(
            array(
                'noback' => TRUE,
                'head'   => '全部线路',
                'title'   => '全部线路',
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/line_city', $this->data);
    }

    public function datetimepicker(){
        $this->data['common'] = array(
            array(
                'noback' => TRUE,
                'head'   => '选择团期',
                'title'   => '选择团期',
            )
        );
        $this->load->view(MOBILE_TEMPLATE_NAME.'/datetimepicker', $this->data);
    }
}